#ifndef CHATSERVER_H
#define CHATSERVER_H

#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDateTime>
#include <QFile>
#include <QDebug>
#include <QCoreApplication>
#include <QCryptographicHash>
#include <QThread>
#include <QSqlError>
class ChatServer : public QObject
{
    Q_OBJECT
public:
    explicit ChatServer(QObject *parent = nullptr);
    ~ChatServer();
    void startServer(quint16 port = 7456);
private slots:
    void newConnectionSlot();
    void readyReadSlot();
    void disconnSlot();
private:
    void printMsg(const QString &msg);
    void connect2DB();
    void createLoginTable();
    void createChatTable();
    bool isDataExists(const QString &id);
    bool isLoginExists(const QString &user_name, const QString &password);
    void selectTime(const QString &dt, int clientIndex);
    void selectNickname(const QString &nickname, QTcpSocket *clientSocket);
    void selectMsg(const QString &msg, QTcpSocket *clientSocket);
    void selectAll(QTcpSocket *clientSocket);
    QString getClientInfo(QTcpSocket *socket) const;
    QTcpServer *server; //服务器
    QList<QTcpSocket*> clients;//客户端
    QSqlDatabase db;//数据库
    QFile logFile;//日志文件
};
#endif // CHATSERVER_H
